import cTitle from "components/title";
import { Loading } from 'element-ui';
import dayjs from 'dayjs';
const topBg = require('../../../assets/images/activity-top-bg@2x.png');
const loadingOptions = {
  background: 'rgba(0, 0, 0, .6)',
  text: '加载中...'
};
export default {
  components: { cTitle },
  data() {
    return {
      topBg,
      frozenIntegral: 0,
      activityList: [],
      getting: false,
      partInSuccessTimer: null,
      checkTime: 0,
    };
  },

  methods: {
    /**
     * 获取活动列表
     */
    getActivityList: function() {
      this.getting = true;
      const that = this;
      $http.get('plugin.luck-new-draw.api.activity.getActivityList', {basic_info: 1}).then(function(response) {
        if (response.result == 1) {
          const { frozen_integral, list } = response.data;
          that.frozenIntegral = frozen_integral;
          that.activityList = list;
        } else {
          that.$dialog.alert({ message: response.msg });
        }
      }).finally(function() {
        that.getting = false;
      });
    },

    /**
     * 查询当前用户是否参加活动中
     * @param {Object} activity 活动数据
     */
    getInActivities: function(activity) {
      const that = this;
      let loadingInstance = Loading.service(loadingOptions); 
      $http.get('plugin.luck-new-draw.api.activity.getInActivities', { activity_id: activity.id}).then(function(response) {
        const { result, data } = response;
        if (result == 1) {
          // 是否在活动中 ：0不在，1当前正在参加
          if (data.is_join == 1) {
            localStorage.removeItem('viewWinningResult');
            that.$router.push(that.fun.getUrl('lucky_draw', {activity_id: data.activity_id, reward_id: data.reward_id}));
          } else if (data.is_join == 0) {
            that.$dialog.confirm({
              title: `确定消耗${activity.integral}积分参加此活动吗？`
            }).then(function() {
              that.partInActivity(data.activity_id);
            }).catch(function() {});
          }
        } else {
          that.$dialog.alert({ message: response.msg || '验证错误' });
        }
      }).finally(function() {
        loadingInstance.close();
      });
    },

    /**
     * 请求参加活动
     * @param {Number} activity_id 活动id
     */
    partInActivity: function(activity_id) {
      const that = this;
      let loadingInstance = Loading.service(loadingOptions); 
      $http.get('plugin.luck-new-draw.api.activity.partInActivity', { activity_id }).then(function(response) {
        if (response.result == 1) {
          // 定时请求是否有活动数据
          that.checkTime = 0;
          that.partInSuccessTimer = setInterval(function() {
            that.partInSuccess(activity_id, response.data.reward_id);
          }, 1000);
          // that.$router.push(that.fun.getUrl('lucky_draw', {activity_id, reward_id}));
        } else {
          that.$dialog.alert({ message: response.msg || '参加活动失败' });
        }
      }).finally(function() {
        loadingInstance.close();
      });
    },

    /**
     * 是否有活动数据
     * @param {String | Number} activity_id 
     * @param {String | Number} reward_id 
     */
    partInSuccess: function(activity_id, reward_id) {
      let loadingInstance = Loading.service(loadingOptions);
      if (this.checkTime >=10) {
        // 查询超过10次不在查询；
        loadingInstance.close();
        clearInterval(that.partInSuccessTimer);
        that.$dialog.alert({ message: '活动数据有误，请联系管理员' });
        return;
      }
      // 查询次数+1
      this.checkTime++;
      const that = this;
      $http.get('plugin.luck-new-draw.api.activity.partInTeam', { activity_id, reward_id, from: 'list' })
        .then(function(response) {
          if (response.result == 1) {
            // status: 1-进行中, 2-已解散, 3-已开奖
            if (response.data.status == 1 || response.data.status == 3) {
              loadingInstance.close();
              clearInterval(that.partInSuccessTimer);
              localStorage.removeItem('viewWinningResult');
              that.$router.push(that.fun.getUrl('lucky_draw', {activity_id, reward_id}));
            }
          }
        });
    },

    /**
     * 参与活动按钮点击
     * @param {Object} activity 活动数据
     */
    participateActivity: function(activity) {
      if(activity.is_satisfy == 0) {
        this.$toast('不满足参加活动的条件');
        return;
      }
      const isOver = dayjs().isBefore(dayjs(activity.end_time));
      if (!isOver) {
        this.$toast('活动已结束');
        return;
      }
      console.log();
      const that = this;
      that.getInActivities(activity);
    },

    /**
     * 跳转到获奖记录
     */
    goAwardRecord: function() {
      this.$router.push(this.fun.getUrl('integral_award_record'));
    }
  },

  activated() {
    this.getActivityList();
  }
};
